@echo off
set OPTS=-ft -autorot -c
set RENAMEOPTS=-n%%%%y%%%%m%%%%d_%%%%H%%%%M%%%%S
set prefix=
set batfile=batfile.tmp.cmd
set sz_0=0
set sz_100=100


rem elso inditaskor elmentjuk az exe helyet... 
set temp=%~dp0jhead.exe
del /q %batfile%
@echo @echo on >%batfile%


:setJpegPath
if exist "%temp%" (
	@echo jpegPath set up, %~dp0jhead.exe exists.
	set jpegPath=%~dp0
	cd /d %~dp1
	call :scanForFiles %1
)
goto end

rem ********************
:scanForFiles
rem ********************
	cls
	@echo Directory: %~dp1 
	@echo File:      %~nx1 
	@echo 1 - Only this file (%~nx1)
	@echo 2 - All files in this directory (%~dp1*.jpg)
	@echo 3 - All files, recursiv (%~dp1**\*.jpg)
	set /p ans_mode="Mode? [2]"
	set /a ans_mode=%ans_mode% + 0 2>nul
	if "%ans_mode%" EQU "0" (
		set ans_mode=2
	)
	if "%ans_mode%" EQU "1" (
		set mode=thisFileOnly
	) else (
		if "%ans_mode%" EQU "2" (
			set mode=allFiles
			call :setPrefix
		) else (
			if "%ans_mode%" EQU "3" (
				set mode=recursiv
			) else (
				set mode=allFiles
				call :setPrefix
			)
		)
	)

	if "%prefix%" NEQ "" (
		set RENAMEOPTS=%RENAMEOPTS%_%prefix%
	)
	set RENAMEOPTS=%RENAMEOPTS%_%%%%f

	REM ************************
	REM                QUALITY
	REM ************************
	set quality=90
	set /p ans_quality="Quality [%quality%]?"
	IF /i "%ans_quality%" EQU "" (		
		set ans_quality=%quality%	
	)

	set /a ans_quality=%ans_quality%
	set quality=%ans_quality%
	rem	@echo Quality is set to: %quality%
	rem	IF /I "%ans_quality%" GTR "%sz_0%" (
	rem		IF /i "%sz_100%" GEQ "%ans_quality%" (
	rem			set quality=%ans_quality%
	rem	)
	@echo Quality is set to: %quality%


	REM ************************
	REM		Resize?
	REM ************************
	set defSize=1600
	set /p ans_resize="Long side? 0:[%defSize%] -1: no resize other number: that size"
	set /a ans_resize=%ans_resize% + 0 2>nul
	rem NO resize
	if "%ans_resize%" EQU "-1" (
		set otherOpts=
	) else (
		rem Size given.
		if "%ans_resize%" NEQ "0" (
			set otherOpts=-ratio -resize %ans_resize% %ans_resize% -rtype lanczos
		rem Default size
		) else (
			set otherOpts=-ratio -resize %defSize% %defSize% -rtype lanczos
		)	
	)

	REM ************************
	REM	Backup?
	REM ************************
	set defBackup=yes
	set backup=%defBackup%

	set /p ans_backup="Create backup? 1:[yes] 0: no"
	if /i "%ans_backup%" EQU "0" (
		set backup=no
	) else (
		if /i "%ans_backup%" EQU "n" (
			set backup=no
		) else (
			if /i "%ans_backup%" EQU "N" (
				set backup=no
			)
		)
	)

		
	REM ************************
	REM	Moving them according their date?
	REM ************************
	set moveFile=no
	set /p ans_move="Move files to separate folders according to its date? [no]"

	if /i "%ans_move%" EQU "1" (
		set moveFile=yes
	) else (
		if /i "%ans_move%" EQU "i" (
			set moveFile=yes
		) else (
			if /i "%ans_move%" EQU y (
				set moveFile=yes
			)
		)
	)


	
	REM ************************
	REM 	Recursiv?
	REM ************************
	@echo.
	@echo Preparing files...
	rem @echo Mode: %mode%
	if "%mode%"=="recursiv" (
		for /R %%a in (*.jpg) DO (
			call :process "%%~da" "%%~pa" "%%~na" "%%~xa"
		)
	)

	if "%mode%"=="allFiles" (
		for %%a in (*.jpg) DO (
			call :process "%%~da" "%%~pa" "%%~na" "%%~xa"
		)
	)

	if "%mode%"=="thisFileOnly" (
		for %%a in (%1) DO (
			call :process "%%~da" "%%~pa" "%%~na" "%%~xa"
		)
	)

	cd /d %jpegPath%
	@echo CD: %cd%
	call %batfile%

	
	goto end

:process

	setlocal
	set d=%~1
	set p=%~2
	set n=%~3
	set x=.jpg
	
	rem Ha a file neveben elol szamok vannak, akkor nem fut ra.
	set isdate=0
	set /a isdate=1%n:~0,6%+0 2>nul
	if "%isdate%" == "0" (
		set endopts=%OPTS% %RENAMEOPTS%
	) else (
		set endopts=%OPTS%
		@echo Skipping to process the file: %d%%p%%n%%x%
		goto end
	)
	set bakPath=%cd%
	cd /d %jpegPath%
	
	@echo. >>%batfile%
	@echo @echo. >>%batfile%
	@echo title Processing %d%%p%%n%%x%...  >>%batfile%
	@echo   %d%%p%%n%%x%...
	@echo Processing file: %d%%p%%n%%x% >>%cd%\log.txt
	rem @echo Processing file: %d%%p%%n%%x%
	rem .\nconvert\nconvert.exe -info "%d%%p%%n%%x%"
	
	if %quality% NEQ %sz_100% (
		if "%backup%" == "yes" (
			@echo @echo  Creating backup... >>%batfile%
			@echo copy "%d%%p%%n%%x%" "%d%%p%%n%%x%.bak" ^>nul >>%batfile%
			@echo @echo  Ok. >>%batfile%
		)
		
		@echo @echo  Optimizing file to %quality%^%%... >>%batfile%
		@echo %cd%\nconvert\nconvert.exe -keepfiledate -rexifthumb -buildexifthumb %otherOpts% -q %quality% -i  -out jpeg "%d%%p%%n%%x%" ^>nul 2^>^>%cd%\log.txt >>%batfile%
		@echo @echo  Ok. >>%batfile%
	)

	@echo @echo Rotating image, renaming file... >>%batfile%
	@echo %cd%\jhead.exe %endopts% "%d%%p%%n%%x%" ^>^>%cd%\log.txt >>%batfile%
	@echo @echo  Ok. >>%batfile%
		
	if "%isdate%" NEQ "0" (
		if "%moveFile%" == "yes" (
			rem @echo  Moving the file according its date...
			@echo mkdir "%d%%p%%n:~0,6%" 2^>nul ^>nul >>%batfile%
			@echo move "%d%%p%%n%%x%" "%d%%p%%n:~0,6%" >>%batfile%
			rem @echo  Ok.
		)
	)	
	
	cd /d %bakPath%
	cd /d %jpegPath%

	endlocal
goto end

:setPrefix
		set prefix=
		set /p prefix="Filename prefix?"
		if "%prefix%" NEQ "" (
			set prefix=%prefix:-= - %
			set prefix=%prefix:  = %
			set prefix=%prefix: =_%
		)
goto end

:end